<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../menu.php"); // redirection en cas d'echec
}
	require_once('../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');
	//require_once('../inclure.php');
	$message = '';
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . addslashes($theValue) . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if( isset($_POST['Ligne1_adresse']) && empty($_POST['Ligne1_adresse']) ){
	$message = colorer("Vous devez saisir la première ligne d’adresse.","false");
}/*elseif(isset($_POST['Ligne1_adresse']) && caracteres_interdits($_POST['Ligne1_adresse'])==1){
	$message = colorer("La première ligne adresse saisie comporte des caractères spéciaux interdits.","false");
}*/
/* // --- modification par t.rabeson@gmail.com le 20/04/2011
elseif( isset($_POST['Ligne2_adresse']) && empty($_POST['Ligne2_adresse']) ){
	$message = colorer("Vous devez renseiger la deuxième ligne de cette adresse.","false");
	//$message =colorer("", "false");
}*/
/*elseif(isset($_POST['Ligne2_adresse']) && caracteres_interdits($_POST['Ligne2_adresse'])==1){
	$message = colorer("La seconde ligne adresse comporte des caractères spéciaux interdits.","false");
}*/elseif( isset($_POST['Ligne3_adresse']) && empty($_POST['Ligne3_adresse']) ){
	$message = colorer("Vous devez saisir au minimum la ville dans la troisième ligne d’adresse.","false");
}/*elseif(isset($_POST['Ligne3_adresse']) && caracteres_interdits($_POST['Ligne3_adresse'])==1){
	$message = colorer("La troisième ligne adresse saisie comporte des caractères spéciaux interdits.","false");
}*/else{
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	  $updateSQL = sprintf("UPDATE adresse SET Type_adresse=%s, Etat_adresse=%s, Ligne1_adresse=%s, Ligne2_adresse=%s, Ligne3_adresse=%s, Identifiant_personne=%s WHERE Identifiant_adresse=%s",
						   GetSQLValueString($_POST['Type_adresse'], "text"),
						   GetSQLValueString($_POST['Etat_adresse'], "text"),
						   GetSQLValueString($_POST['Ligne1_adresse'], "text"),
						   GetSQLValueString($_POST['Ligne2_adresse'], "text"),
						   GetSQLValueString($_POST['Ligne3_adresse'], "text"),
						   GetSQLValueString($_POST['Identifiant_personne'], "int"),
						   GetSQLValueString($_POST['Identifiant_adresse'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	  mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  	mysql_query("commit");
		$message = colorer("La modification de cette adresse est prise en compte.","true");
	  }else{
		$message = colorer("Problème modification adresse, contacter la DSI.","false");
		mysql_query("rollback");
	  }
	}
}

// Suppression de l'adresse (rendre l'état de l'adresse Inactif : I)
if(isset($_GET["supp"]) && $_GET["supp"]=="1" && isset($_GET["Identifiant_adresse"]) && isset($_GET["Identifiant_personne"])){
	//var_dump($_GET);
	$nombreAdrActive = intval(nombreAdrActive($database_microassur, $microassur, $_GET['Identifiant_personne']));
	//var_dump($nombreAdrActive);
	if($nombreAdrActive<=1){ // Vérifier si le partenaire a au moins 2 adresse actives (état à A)
		$deleteGoTo = "index.php?Identifiant_personne=".$_GET['Identifiant_personne']."&msg=no";
		header(sprintf("Location: %s", $deleteGoTo));
	}else{
		$_POST['Etat_adresse'] = 'I';
		$updateSQL2 = "UPDATE adresse SET Etat_adresse='I' WHERE Identifiant_adresse=" . GetSQLValueString($_GET['Identifiant_adresse'], "int") . " ;";
		mysql_select_db($database_microassur, $microassur);
		mysql_query("START TRANSACTION");
		$Result2 = mysql_query($updateSQL2, $microassur) or die(mysql_error());	
		if($Result2){
			mysql_query("commit");
			//$message = colorer("Adresse supprimée.","true");
			$deleteGoTo = "./index.php?Identifiant_adresse=". $_GET['Identifiant_adresse'] ."&Identifiant_personne=". $_GET['Identifiant_personne'] ."&msg=supp";
			header(sprintf("Location: %s", $deleteGoTo));
		}else{
			mysql_query("rollback");
			$message = colorer("Problème suppression adresse, contacter la DSI .","false");
		}
	} // Fin else $nombreAdrActive<=2
}


$colname_rs_mod = "-1";
if (isset($_GET['Identifiant_adresse'])) {
  $colname_rs_mod = $_GET['Identifiant_adresse'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_mod = sprintf("SELECT * FROM adresse WHERE Identifiant_adresse = %s", GetSQLValueString($colname_rs_mod, "int"));
$rs_mod = mysql_query($query_rs_mod, $microassur) or die(mysql_error());
$row_rs_mod = mysql_fetch_assoc($rs_mod);
$totalRows_rs_mod = mysql_num_rows($rs_mod);

mysql_select_db($database_microassur, $microassur);
$query_rs_personne = "SELECT * FROM personne";
$rs_personne = mysql_query($query_rs_personne, $microassur) or die(mysql_error());
$row_rs_personne = mysql_fetch_assoc($rs_personne);
$totalRows_rs_personne = mysql_num_rows($rs_personne);

/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="M0004"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = '$num_row'";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Modification Partenaire</title>
			<link rel="stylesheet" type="text/css" href="../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
<div id="sf_menu1">
	<h3>Partenaire <?php //echo nomPersonne($database_microassur, $microassur, $_GET['Identifiant_personne']) ?></h3>
<?php
	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Gestion partenaire";
	//$nommodule2="Liste des partenaires";
	$nom_module_saisie="Détail adresse";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<!--/***************************************/-->

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td>TYPE ADRESSE</td>
      <td><select name="Type_adresse">
			 <?php do { ?>
					<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php if(isset($_POST['Type_adresse'])){if (!(strcmp($row_rs_dico2['Code_element'], htmlentities($_POST['Type_adresse'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}}else{ if (!(strcmp($row_rs_dico2['Code_element'], htmlentities($row_rs_mod['Type_adresse'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";}} ?> ><?php echo $row_rs_dico2['Nom_element']; ?></option>
			<?php } while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
      </select>
      </td>
    </tr>
    <input type="hidden" name="Etat_adresse" value="<?php echo htmlentities($row_rs_mod['Etat_adresse'], ENT_COMPAT, 'utf-8') ?>" />
    <tr>
      <td>ADRESSE</td>
      <td><input type="text" name="Ligne1_adresse" value="<?php if(isset($_POST['Ligne1_adresse'])){ echo remind_text('Ligne1_adresse'); }else{ echo htmlentities(stripslashes($row_rs_mod['Ligne1_adresse']), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="text" name="Ligne2_adresse" value="<?php if(isset($_POST['Ligne2_adresse'])){ echo remind_text('Ligne2_adresse'); }else{ echo htmlentities(stripslashes($row_rs_mod['Ligne2_adresse']), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="text" name="Ligne3_adresse" value="<?php if(isset($_POST['Ligne3_adresse'])){ echo remind_text('Ligne3_adresse'); }else{ echo htmlentities(stripslashes($row_rs_mod['Ligne3_adresse']), ENT_COMPAT, 'utf-8');} ?>" size="32" maxlength="30" /></td>
    </tr>
	<input type="hidden" name="Identifiant_personne" value="<?php echo htmlentities($row_rs_mod['Identifiant_personne'], ENT_COMPAT, 'utf-8'); ?>" />
  </table>
    </div>
</div>
<div class="imgright"></div>				
<div class="cleared"></div>

<div class="bt1">
	<a href="index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Retour</span>
        <span class="btright"></span>
    </a>
</div>

<div class="bt">
	<a href="../adresse/index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
</div>
<!--div class="bt">
	<a href="modadresse.php?Identifiant_adresse=<?php //echo $row_rs_mod['Identifiant_adresse']; ?>&Identifiant_personne=<?php //echo $row_rs_mod['Identifiant_personne']; ?>&supp=1">
	<span class="btleft"></span>
    <span class="btmiddle">Supprimer</span>
    <span class="btright"></span>
    </a>
</div-->
<div class="bt">
	<span class="btleft"></span>
    <input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span>
</div>
    
<div class="cleared"></div>

  <input type="hidden" name="Identifiant_adresse" value="<?php echo $row_rs_mod['Identifiant_adresse']; ?>" />
  <input type="hidden" name="MM_update" value="form1" />
  <!--input type="hidden" name="Identifiant_adresse" value="<?php //echo $row_rs_mod['Identifiant_adresse']; ?>" /-->
</form>
 </div>
</body>
</html>
<?php
mysql_free_result($rs_mod);

mysql_free_result($rs_personne);
ob_end_flush();
?>
